#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF_SIZE 1024*1

void image_copy(const char *path,const char *path1)
{
    //打开第一张图片
    FILE *fp = fopen(path,"rb");
    if(fp == NULL)
    {
        perror("fopen image fail");
        exit(1);
    }
    
    //打开要复制的图片
    FILE *fp1 = fopen(path1,"wb+");
    if(fp1 == NULL)
    {
        perror("fopen image1 fail");
        exit(1);
    }

    //保存第一张图片的数据
    //读一次，写一次，不需要给buf那么大空间
    int ret_r,ret_w;
    char *pbuf = (char *)malloc(sizeof(char) * BUF_SIZE) ;
    memset(pbuf, 0, 1024);
    //图片不超过1MB
    while( ret_r = fread(pbuf, 1, BUF_SIZE, fp) > 0)
    {
        //将第一张图片的所有数据写入第二张图片
        ret_w = fwrite(pbuf, 1, BUF_SIZE,fp1);
        if(ret_w == 0)
        {
            perror("fwrite fail");
            exit(1);
        }

    }
    printf("图片复制完毕\n");
    pclose(fp);
    pclose(fp1);
    
}

int main(int argc, char **argv)
{
    if(argc != 3)
    {
        printf("./image_cp xxx.bmp xxx.bmp\n");
        exit(1);
    }

    image_copy(argv[1],argv[2]);

    return 0;
}

